//
//  YWNotice.m
//  maintain
//
//  Created by Kyo on 16/3/4.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNotice.h"
#import "MJExtension.h"
#import "YWDepartment.h"
#import "YWOrganization.h"
#import "YWHandler.h"

@implementation YWNotice

+ (NSDictionary*)mj_replacedKeyFromPropertyName
{
    return @{ @"noticeId" : @"id",
              @"fromUser" : @"releaseUser"
              };
}

+ (NSDictionary*)mj_objectClassInArray
{
    return @{ @"imagesList" : @"YWPatrolPicture",
              @"attachmentList" : @"YWattachment",
              @"observers" : @"YWHandler",
              @"departmentReceivers" : @"YWDepartmentJson",
              @"organizationReceivers" : @"YWOrganization"};
}

+ (NSArray *)mj_ignoredPropertyNames
{
    return @[@"receivers",@"receiverIds",@"isExpand",@"observerIds",@"organizationNos"];
}

- (void)mj_keyValuesDidFinishConvertingToObject{
    NSMutableString *atUserName = [[NSMutableString alloc] init];
    [self.observers enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (atUserName.length && idx!=0) {
            [atUserName appendString:@","];
        }
        [atUserName appendString:[@(handler.uniqueId) stringValue]];
    }];
    self.observerIds = [NSString stringWithFormat:@"%@",atUserName];
    
    NSMutableString *names = [[NSMutableString alloc] init];
    NSMutableString *organizationNos = [[NSMutableString alloc] init];
    if (self.organizationReceivers.count>0) {
        [self.organizationReceivers enumerateObjectsUsingBlock:^(YWOrganization*  _Nonnull organization, NSUInteger idx, BOOL * _Nonnull stop) {
            if (!organization.simpleName) {
                organization.simpleName = organization.organizationName;
            }
            if (organization.simpleName.length>0) {
                if (idx!=0) {
                    [names appendString:@","];
                }
                [names appendString:organization.simpleName];
            }
            if (idx!=0) {
                [organizationNos appendString:@","];
            }
            [organizationNos appendString:organization.organizationNo];
        }];
    }
    NSMutableString *receiverIds = [[NSMutableString alloc] init];
    if (self.departmentReceivers.count>0) {
        [self.departmentReceivers enumerateObjectsUsingBlock:^(YWDepartment*  _Nonnull department, NSUInteger idx, BOOL * _Nonnull stop) {
            if (department.departmentName.length>0) {
                if (idx!=0) {
                    [names appendString:@","];
                }
                [names appendString:department.departmentName];
            }
            if (idx!=0) {
                [receiverIds appendString:@","];
            }
            [receiverIds appendString:[@(department.departmentId) stringValue]];
        }];
    }
    self.receivers = [NSString stringWithFormat:@"%@",names];
    self.receiverIds = [NSString stringWithFormat:@"%@",receiverIds];
    self.organizationNos = [NSString stringWithFormat:@"%@",organizationNos];
}
@end

